<?php
/**
 * 法拍百科点赞模型
 */

namespace app\api\model;


use app\common\model\BaseModel;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Model;

class WikiGive extends BaseModel {

    /**
     * 查询用户是否点赞
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function see($params) {
        $data = db('wiki_give_agent')->where('agent_id', $params['agent_id'])->where('wiki_id', $params['wiki_id'])->select();
        if (empty($data)) {
            self::add($params['agent_id'], $params['wiki_id']);
            return 1;
        } else {
            self::del($params['agent_id'], $params['wiki_id']);
            return 2;
        }
    }

    /**
     * 添加
     * @param $user_id
     * @param $wiki_id
     * @return int|string
     */
    public static function add($agent_id, $wiki_id) {
        return db('wiki_give_agent')->insert(['wiki_id' => $wiki_id, 'agent_id' => $agent_id]);
    }

    /**
     * 删除
     * @param $user_id
     * @param $wiki_id
     * @return int
     * @throws Exception
     * @throws PDOException
     */
    public static function del($agent_id, $wiki_id) {
        return db('wiki_give_agent')->where('agent_id', $agent_id)->where('wiki_id', $wiki_id)->delete();
    }

    /**
     * 是否点赞
     * @param $user_id
     * @param $wiki_id
     * @return array|PDOStatement|string|Model|null
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function is_has($user_id, $wiki_id) {
        return db('wiki_give_agent')->where('agent_id', $user_id)->where('wiki_id', $wiki_id)->find();
    }

}
